<?php

namespace App\Model;

use App\User;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Http\Request;

/**
 * Class Partner
 *
 * @package App\Model
 * @property User $user
 * @method static bool|null forceDelete()
 * @method static \Illuminate\Database\Query\Builder|\App\Model\Partner onlyTrashed()
 * @method static bool|null restore()
 * @method static \Illuminate\Database\Query\Builder|\App\Model\Partner withTrashed()
 * @method static \Illuminate\Database\Query\Builder|\App\Model\Partner withoutTrashed()
 * @mixin \Eloquent
 * @property-read \App\Model\Places $provinces
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Model\Partner newModelQuery()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Model\Partner newQuery()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Model\Partner query()
 * @property int $id 主键
 * @property int $pid 上级所属
 * @property int $aid admin对应
 * @property int $user_id ->users
 * @property int $province 省
 * @property int $city 市
 * @property int $area 区
 * @property string $title 合伙人名称
 * @property string $admin_name 负责人姓名
 * @property string $admin_mobile_phone 负责人电话
 * @property string $id_card 负责人身份证
 * @property string $bank_card_no 银行卡号
 * @property string $bank_card_name 银行卡账户名
 * @property string $bank_name 银行卡开户行
 * @property string $bank_branch 银行卡支行
 * @property int $operator_count 运营商数量
 * @property int $salesman_count 业务员数量
 * @property int $user_count 用户数量
 * @property int $vip_count 会员数量
 * @property \Illuminate\Support\Carbon $created_at 创建时间
 * @property \Illuminate\Support\Carbon $updated_at 创建时间
 * @property string|null $deleted_at 删除时间
 * @property int $status 状态0启用1弃用
 * @property int $operators_num 可添加的运营商数量
 * @property string $business_license 营业执照
 * @property string $id_card_front 身份证正面
 * @property string $id_card_back 身份证反面
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Model\Partner whereAdminMobilePhone($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Model\Partner whereAdminName($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Model\Partner whereAid($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Model\Partner whereArea($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Model\Partner whereBankBranch($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Model\Partner whereBankCardName($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Model\Partner whereBankCardNo($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Model\Partner whereBankName($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Model\Partner whereBusinessLicense($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Model\Partner whereCity($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Model\Partner whereCreatedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Model\Partner whereDeletedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Model\Partner whereId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Model\Partner whereIdCard($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Model\Partner whereIdCardBack($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Model\Partner whereIdCardFront($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Model\Partner whereOperatorCount($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Model\Partner whereOperatorsNum($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Model\Partner wherePid($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Model\Partner whereProvince($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Model\Partner whereSalesmanCount($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Model\Partner whereStatus($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Model\Partner whereTitle($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Model\Partner whereUpdatedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Model\Partner whereUserCount($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Model\Partner whereUserId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Model\Partner whereVipCount($value)
 */
class Partner extends Model
{
    use SoftDeletes;
    protected $table = 'place_partners';

    protected $fillable = ['pid', 'aid', 'province', 'city', 'area', 'title', 'bank_branch', 'admin_name', 'admin_mobile_phone', 'id_card', 'bank_card_no', 'bank_card_name', 'bank_name','operators_num', 'business_license','id_card_front','id_card_back'];

    protected $hidden = ['deleted_at', 'pid', 'aid', 'provinces'];

    protected $appends = [];

    public function provinces()
    {
        return $this->hasOne(Places::class, 'id', 'province');
    }

    public function user()
    {
        return $this->hasOne(User::class, 'user_id', 'id');
    }
    /**
     * 根据条件分页
     * @param Request $request
     * @return \Illuminate\Contracts\Pagination\LengthAwarePaginator
     */
    public function getPaginate(Request $request)
    {
        $limit = request('limit', 15);
        $where = [];
        if ($request->has('province')) {
            $where['province'] = $request->input('request');
        }
        if ($request->has('city')) {
            $where['city'] = $request->input('city');
        }
        if ($request->has('admin_name')) {
            $where['admin_name'] = $request->input('admin_name');
        }
        if ($request->has('admin_mobile_phone')) {
            $where['admin_mobile_phone'] = $request->input('admin_mobile_phone');
        }
        if ($request->user()->type > 0) {
            $pid = $request->user()->id;//上级id，当前登录用户id
            $where['pid'] = $pid;
        }
        return $this->where($where)->paginate($limit)->toArray();
    }
}
